package com.longway.common.kit;

import java.util.Random;

public class RandomKit {

	private static final Random RANDOM = new Random();

	public static String random(int count) {
		return random(count, false, false);
	}

	public static String randomAscii(int count) {
		return random(count, 32, 127, false, false);
	}

	public static String randomAlphabetic(int count) {
		return random(count, true, false);
	}

	public static String randomAlphanumeric(int count) {
		return random(count, true, true);
	}

	public static String randomNumeric(int count) {
		return random(count, false, true);
	}

	public static String random(int count, boolean letters, boolean numbers) {
		return random(count, 0, 0, letters, numbers);
	}

	public static String random(int count, int start, int end, boolean letters, boolean numbers) {
		return random(count, start, end, letters, numbers, null, RANDOM);
	}

	public static String random(int count, int start, int end, boolean letters, boolean numbers, char chars[]) {
		return random(count, start, end, letters, numbers, chars, RANDOM);
	}

	public static String random(int count, int start, int end, boolean letters, boolean numbers, char chars[],
			Random random) {
		if (count == 0) {
            return "";
        }
		if (count < 0) {
            throw new IllegalArgumentException((new StringBuilder()).append("Requested random string length ")
                    .append(count).append(" is less than 0.").toString());
        }
		if (chars != null && chars.length == 0) {
            throw new IllegalArgumentException("The chars array must not be empty");
        }
		if (start == 0 && end == 0) {
			if (chars != null) {
                end = chars.length;
            } else if (!letters && !numbers) {
				end = 2147483647;
			} else {
				end = 123;
				start = 32;
			}
		} else if (end <= start) {
            throw new IllegalArgumentException((new StringBuilder()).append("Parameter end (").append(end)
                    .append(") must be greater than start (").append(start).append(")").toString());
        }
		char buffer[] = new char[count];
		int gap = end - start;
		while (count-- != 0) {
			char ch;
			if (chars == null) {
                ch = (char) (random.nextInt(gap) + start);
            } else {
                ch = chars[random.nextInt(gap) + start];
            }
			if (letters && Character.isLetter(ch) || numbers && Character.isDigit(ch) || !letters && !numbers) {
				if (ch >= '\uDC00' && ch <= '\uDFFF') {
					if (count == 0) {
						count++;
					} else {
						buffer[count] = ch;
						count--;
						buffer[count] = (char) (55296 + random.nextInt(128));
					}
				} else if (ch >= '\uD800' && ch <= '\uDB7F') {
					if (count == 0) {
						count++;
					} else {
						buffer[count] = (char) (56320 + random.nextInt(128));
						count--;
						buffer[count] = ch;
					}
				} else if (ch >= '\uDB80' && ch <= '\uDBFF') {
                    count++;
                } else {
                    buffer[count] = ch;
                }
			} else {
				count++;
			}
		}
		return new String(buffer);
	}

	public static String random(int count, String chars) {
		if (chars == null) {
            return random(count, 0, 0, false, false, null, RANDOM);
        } else {
            return random(count, chars.toCharArray());
        }
	}

	public static String random(int count, char chars[]) {
		if (chars == null) {
            return random(count, 0, 0, false, false, null, RANDOM);
        } else {
            return random(count, 0, chars.length, false, false, chars, RANDOM);
        }
	}
	
	/**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length,boolean isNumber) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        if(isNumber){
        	 base = "0123456789";
        }
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
